//
//  WelcomeViewController.swift
//  01_新浪微博
//
//  Created by 黄蓉 on 15/12/16.
//  Copyright © 2015年 黄蓉. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class WelcomeViewController: UIViewController {
///  直接替换控制器额view
    override func loadView() {
        view = backgroundImageView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showAnimation()
    }
    
    ///设置UI界面
    func setupUI() {
         view.addSubview(nameLabel)
        view.addSubview(iconView)
       iconView.layer.cornerRadius = 45
        iconView.clipsToBounds = true
        
        //设置iconView的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(CGSize(width: 90, height: 90))
            make.centerX.equalTo(view.snp_centerX)
            make.centerY.equalTo(view.snp_centerY).offset(200)
        }
        //设置label的约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(15)
          
        }
        
        //设置头像图片
        iconView.sd_setImageWithURL(UserAccountModel().headUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    
    ///头像view
        lazy var iconView : UIImageView = UIImageView()
    ///背景view
        lazy var backgroundImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///name的label
        lazy var nameLabel : UILabel = UILabel(title: UserAccountModel().uerName ?? "" + "欢迎归来", color:  UIColor.lightGrayColor(), fontSize: 14)
 
    
    ///展示头像的动画
    private func showAnimation() {
  nameLabel.alpha = 0
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            //更新iconView的Y值
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.centerY.equalTo(self.view.snp_centerY).offset(-200)
            })
            //强制刷新
            self.view.layoutIfNeeded()
           
            }) { (_) -> Void in
                //在头像的动画结束后,执行label的动画,显得画面有层次感
              UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.nameLabel.alpha = 1.0
                }, completion: { (_) -> Void in
            NSNotificationCenter.defaultCenter().postNotificationName(notification, object: nil)
              })
        }
    }
}